package com.example.pungolo.journal;

import java.io.File;

import com.example.pungolo.journal.view.BitmapWorkerTask;
import com.example.pungolo.journal.view.CroppableImageView;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Point;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class CropActivity extends Activity {

	private CroppableImageView image;
	
  AlertDialog.Builder cancelAlertBuilder;
  int[] oldCropData;

  @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_crop);
		Intent caller=getIntent();
		
    if (caller == null || !caller.hasExtra("picturePath"))
      throw new IllegalArgumentException();
    String path = caller.getStringExtra("picturePath");
    if (path == null || !(new File(path).exists()))
      throw new IllegalArgumentException();
    
    if (caller.hasExtra("cropData"))
      oldCropData = caller.getIntArrayExtra("cropData");
    if (savedInstanceState != null && savedInstanceState.containsKey("cropData"))
      oldCropData = savedInstanceState.getIntArray("cropData");
    if (oldCropData == null)
      oldCropData = new int[4];
    

		this.image = (CroppableImageView)findViewById(R.id.cropUI_imageView);
    this.image.setCropData(oldCropData);
    loadBitmap(path);
	  
    Button saveButton = (Button) findViewById(R.id.cropUI_savebutton);
    Button cancelButton = (Button) findViewById(R.id.cropUI_cancelButton);

    saveButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { onSaveClicked(); } });
    cancelButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { onBackPressed(); } });
	
    cancelAlertBuilder = new AlertDialog.Builder(this);
    cancelAlertBuilder.setMessage("Are you sure you want to lose all the changes you made?");
    cancelAlertBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface arg0, int arg1) {
        onCancelClicked();
      }
    });
    cancelAlertBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface arg0, int arg1) {
      }
    });
  
  }
  protected void onSaveInstanceState(Bundle icicle) {
    super.onSaveInstanceState(icicle);
    this.image.getCropData(oldCropData);
    icicle.putIntArray("cropData", oldCropData);
  }
  
  protected void onCancelClicked() {
    super.onBackPressed();
  }
  
  public void onBackPressed() {
    
    
    
    if (cropDataChanged())
      cancelAlertBuilder.show();
    else {
      super.onBackPressed();
      
    }
  }
  
  private void loadBitmap(final String path) {
    image.post(new Runnable() {
      public void run() {
        Point size = new Point(image.getWidth(), image.getHeight());
        BitmapWorkerTask task = new BitmapWorkerTask(image, size, null);
        task.execute(path);
      }
    });
  }
  
  private boolean cropDataChanged(){
    if (oldCropData == null) return true; 
    int[] newCropData = new int[4];
    this.image.getCropData(newCropData);
    for(int i=0; i<newCropData.length; i++)
    {
      if(newCropData[i] != oldCropData[i])
        return true;
    }
    return false;
    
  }

  protected void onSaveClicked() {
    Intent back = new Intent();
    int[] cropData = new int[4];
    this.image.getCropData(cropData);
    back.putExtra("cropData", cropData);
    
    setResult(RESULT_OK, back);
    finish();
  }

}
